package com.own.demo.utils;

import com.alibaba.fastjson.JSONArray;
import lombok.Data;
import org.locationtech.jts.geom.Coordinate;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author heguanglong
 */
public class TrackTimeSpeedDistUtils {
    /**
     *
     * @param points 轨迹点数量
     * @param distance 总长度
     * @param avgSpeed 平均速度 米/秒
     * @return
     */
    public static List<TimeSpeedDistVo> getHistoryTime(List<ArrayList> points,double distance,int avgSpeed,long allTime){
        List<TimeSpeedDistVo> dataInfo=new ArrayList<>(1024);
        if(points == null || points.size()==0){
            return dataInfo;
        }

        ArrayList lonLat01=points.get(0);
        Date newTime=new Date();
        TimeSpeedDistVo timeSpeedDistVo=new TimeSpeedDistVo(lonLat01,avgSpeed,newTime);
        dataInfo.add(timeSpeedDistVo);

        int row=points.size();
        double allDistance=0;
        long time=0;
        for(int i=1;i<row;i++){
            ArrayList<BigDecimal> upList=points.get(i-1);
            ArrayList<BigDecimal> newList=points.get(i);
            double lon1=upList.get(0).doubleValue();
            double lat1=upList.get(1).doubleValue();
            double lon2=newList.get(0).doubleValue();
            double lat2=newList.get(1).doubleValue();
            double dist=  new BigDecimal(GeoTools.distance(new Coordinate(lon1,lat1),new Coordinate(lon2,lat2))*100000).setScale(3,BigDecimal.ROUND_HALF_UP).longValue();
            double timeTemp=  new BigDecimal(dist/avgSpeed).setScale(3,BigDecimal.ROUND_HALF_UP).doubleValue();
            time=(long)(timeTemp*1000)+time;
            allDistance=allDistance+dist;



            newTime =  DateUtils.getAddTime(newTime,(int)timeTemp);
            TimeSpeedDistVo speedDistVo=new TimeSpeedDistVo(newList,avgSpeed,newTime);
            dataInfo.add(speedDistVo);
        }
        System.out.println("总长度；"+allDistance+",传入总长度："+distance);
        System.out.println("总时长；"+time+",传入总时长："+allTime);
        return dataInfo;
    }

    public static void main(String[] args) {
        String data="[[113.912639,28.401261,0],[113.91095,28.40295,0],[113.9069,28.3989,0],[113.8907,28.3989,0],[113.8826,28.3908,0],[113.8745,28.3827,0],[113.8664,28.3746,0],[113.8583,28.3665,0],[113.8502,28.3584,0],[113.8421,28.3503,0],[113.837674,28.3503,0],[113.834,28.3503,0],[113.834,28.3422,0],[113.834,28.3341,0],[113.834,28.326,0],[113.834,28.3179,0],[113.834,28.3098,0],[113.834,28.3017,0],[113.834,28.2936,0],[113.82995,28.28955,0],[113.829169,28.29033,0],[113.82995,28.28955,0],[113.8259,28.2855,0],[113.8178,28.2774,0],[113.8178,28.2693,0],[113.8178,28.2612,0],[113.8016,28.2612,0],[113.7935,28.2693,0],[113.741583,28.2693,0],[113.5748,28.2693,0],[113.5667,28.2612,0],[113.566281,28.261619,0],[113.5586,28.2693,0],[113.5505,28.2774,0],[113.5424,28.2855,0],[113.5343,28.2936,0],[113.5262,28.3017,0],[113.5181,28.3098,0],[113.51,28.3179,0],[113.5019,28.326,0],[113.4938,28.3341,0],[113.4857,28.3422,0],[113.0969,28.3422,0],[113.094834,28.340134,0],[113.0888,28.3341,0],[112.9349,28.3341,0],[112.9268,28.326,0],[112.9187,28.3179,0],[112.9106,28.3098,0],[112.9025,28.3017,0],[112.8944,28.2936,0],[112.8863,28.2855,0],[112.8782,28.2774,0],[112.8701,28.2693,0],[112.86605,28.26525,0],[112.864508,28.266792,0],[112.86605,28.26525,0],[112.862,28.2612,0],[112.8539,28.2531,0],[112.8458,28.245,0],[112.8377,28.2369,0],[112.8377,28.2288,0],[112.8377,28.2207,0],[112.8377,28.2126,0],[112.8377,28.2045,0],[112.8377,28.1964,0],[112.8377,28.1883,0],[112.8296,28.1802,0],[112.8296,28.1721,0],[112.8296,28.164,0],[112.8134,28.164,0],[112.8053,28.1559,0],[112.7648,28.1559,0],[112.7567,28.1478,0],[112.6271,28.1478,0],[112.628029,28.148729,0],[112.6271,28.1478,0],[112.619,28.1559,0],[112.6109,28.164,0],[112.4489,28.164,0],[112.4489,28.168803,0]]";
        List<ArrayList> list= JSONArray.parseArray(data,ArrayList.class);
        int avg=30;
        double distance=175903.806;
        List<TimeSpeedDistVo> dataInfo=getHistoryTime(list,distance,avg,6332538);
        dataInfo.forEach(a->{
            try {
                System.out.println(DateUtils.parseDateToStr(a.time)+" "+a.getLonLat().get(0)+","+a.getLonLat().get(1));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        });
    }
}

@Data
class TimeSpeedDistVo{
    public ArrayList lonLat;
    public int speed;
    public Date time;
    public long dist;

    TimeSpeedDistVo(ArrayList lonLat,int speed,Date time){
        this.lonLat=lonLat;
        this.speed=speed;
        this.time=time;
    }
}
